using System;
using System.Web.Mvc;
using StructureMap;

namespace ilgolosone.DependencyResolver.SM
{
    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        private readonly IContainer _container;

        public StructureMapControllerFactory(IContainer container)
        {
            _container = container;
        }

        protected override IController GetControllerInstance(Type controllerType)
        {
            if (controllerType == null) return null;
            var controller = _container.GetInstance(controllerType) as Controller;
            if (controller == null) throw new ApplicationException(string.Format("Controller non trovato:{0}", controllerType.Name));
            return controller;
        }
    }
}